package com.headingdata.string;

public class StringDemo2 {

    public static void main(String[] args) {
        //熟悉String 的常用方法
        String s = "中海庭Java";

        //1.字符串长度
        int length = s.length();
        System.out.println(length);

        //2.索引处字符
        char c = s.charAt(1);
        System.out.println(c);

        //3.字符串遍历
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            System.out.print(ch + "\t");
        }
        System.out.println();
        System.out.println("---------------------------------------------------------------------");

        //4.字符串变成字符数组
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            System.out.print(chars[i] + "\t");
        }
        System.out.println();
        System.out.println("----------------------------------------------------------------------");

        //5.判断字符串内容，内容一样就返回true;
        String s1 = new String("中海庭");
        String s2 = new String("中海庭");
        System.out.println(s1 == s2);
        boolean isSame = s1.equals(s2);
        System.out.println(isSame);
        System.out.println("------------------------------------------------------------------------");

        //6.忽略大小写比较字符串内容
        String s3 = "abc123";
        String s4 = "ABc123";
        System.out.println(s3.equals(s4));
        System.out.println(s3.equalsIgnoreCase(s4));
        System.out.println("------------------------------------------------------------------------");

        //7.截取字符串(包前不包后)
        String s5 = "Java是最好的编程语言之一";
        String newS5 = s5.substring(0,12);
        System.out.println(newS5);

        //8.截取字符串，从当前索引到最后
        String s6 = s5.substring(5);
        System.out.println(s6);
        System.out.println("-------------------------------------------------------------------------");

        //9.把字符串中的某个内容替换成新内容，并返回新内容
        String s7 = "这个电影真是个垃圾，垃圾电影，劝大家别看！";
        String s8 = s7.replace("垃圾","**");
        System.out.println(s8);
        System.out.println("-----------------------------------------------------------------------");

        //10.判断字符串是否包含某个关键字
        String s9 = "Java是世界上最好的编程语言之一，我爱Java,Java爱我";
        System.out.println(s9.contains("Java"));
        System.out.println(s9.contains("java"));
        System.out.println(s9.contains("Java是"));
        System.out.println("-----------------------------------------------------------------------");

        //11.判断字符串是否以某个字符串开头
        String s10 = "张三丰";
        System.out.println(s10.startsWith("张"));
        System.out.println(s10.startsWith("张三"));
        System.out.println(s10.startsWith("张四"));
        System.out.println(s10.startsWith("张", 0));
        System.out.println("-----------------------------------------------------------------------");

        //12.字符串拆分
        String s11 = "张无忌，赵敏，周芷若，小昭";
        String[] res1 = s11.split("，");
        for (int i = 0; i < res1.length; i++) {
            System.out.println(res1[i]);
        }


    }
}
